package com.cxy.utils;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

public class ExcelUtil {

    public static void solveRequestHead(String fileName, HttpServletResponse response, HttpServletRequest request) throws IOException {
        //获得浏览器代理信息
        final String userAgent = request.getHeader("USER-AGENT");
        //判断浏览器代理并分别设置响应给浏览器的编码格式
        String finalFileName = null;
        //IE浏览器
        if (StringUtils.contains(userAgent, "MSIE") ||StringUtils.contains(userAgent, "Trident")) {
            finalFileName = URLEncoder.encode(fileName, "UTF8");
            System.out.println("IE浏览器");
            //google,火狐浏览器
        } else if (StringUtils.contains(userAgent, "Mozilla")) {
            finalFileName = new String(fileName.getBytes(), "ISO8859-1");
        } else {
            finalFileName = URLEncoder.encode(fileName, "UTF8");//其他浏览器
        }
        //告知浏览器下载文件，而不是直接打开，浏览器默认为打开
        response.setContentType("application/x-download");
        //下载文件的名称
        response.addHeader("Content-Disposition", "attachment;filename=\"" + finalFileName + "\"");

    }

}
